<template>
  <div class="systemset-business-product-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button v-if="queryForm.upid > 0" @click="backupid">
          返回上级
        </el-button>
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <!-- <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="产品ID"
        prop="ProductID"
        show-overflow-tooltip
        width="70"
      />
      <el-table-column
        align="center"
        label="业务类别"
        prop="BusinessClassName"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column v-if="queryForm.upid == 0" align="center" label="产品类">
        <template #default="{ row }">
          <el-button text @click="getProduct(row)">
            {{ row.ProductName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-else
        align="center"
        label="产品类"
        prop="upProductName"
      />
      <el-table-column
        v-if="queryForm.upid !== 0"
        align="center"
        label="产品名称"
      >
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">
            {{ row.ProductName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="queryForm.upid !== 0"
        align="center"
        label="产品价格"
        width="120"
      >
        <template #default="{ row }">
          <el-text v-if="queryForm.upid > 0">
            {{ row.ProductPrice }}
          </el-text>
          <el-text v-else>---</el-text>
        </template>
      </el-table-column>
      <el-table-column
        v-if="queryForm.upid !== 0"
        align="center"
        label="成本"
        width="120"
      >
        <template #default="{ row }">
          <el-text v-if="queryForm.upid > 0">
            {{ row.ProductCost }}
          </el-text>
          <el-text v-else>---</el-text>
        </template>
      </el-table-column>
      <el-table-column
        v-if="queryForm.upid !== 0"
        align="center"
        label="单位"
        width="120"
      >
        <template #default="{ row }">
          <el-text v-if="queryForm.upid > 0">
            {{ row.ProductUnit }}
          </el-text>
          <el-text v-else>---</el-text>
        </template>
      </el-table-column>
      <el-table-column
        v-if="queryForm.upid == 0"
        align="center"
        label="工作任务设置"
        width="200"
      >
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="taskSetting(row)">
            设置
          </el-button>
        </template>
      </el-table-column>
      <el-table-column v-else align="center" label="任务量限制" width="200">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="taskQuantity(row)">
            任务量限制
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否启用">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
            @click="handleState(row)"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">修改</el-button>
          <!-- <el-button text @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <!-- 工作任务详情的弹窗列表 -->
    <taskEditList ref="taskEditListRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doState, doDelete } from '@/api/systemsetBusinessProduct'
  import Edit from './components/SystemsetBusinessProductEdit'
  import taskEditList from './components/TaskSettingEditList'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'SystemsetBusinessProduct',
    components: { Edit, taskEditList },
    setup() {
      const route = useRoute()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        taskEditListRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          productType: 0,
          classid: 0,
          upid: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })
      const backupid = () => {
        state.queryForm.upid = 0
        state.queryForm.classid = 0
        fetchData()
      }
      const getProduct = (row) => {
        state.queryForm.upid = row.ProductID
        state.queryForm.classid = row.BusinessClassID
        fetchData()
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.ProductID) {
          state.editRef.showEdit(row.BusinessClassID, row.upProductID, row)
        } else {
          state.editRef.showEdit(
            state.queryForm.classid,
            state.queryForm.upid,
            {}
          )
        }
      }
      const handleState = (row) => {
        if (row.ProductID) {
          $baseConfirm('你确定要关闭当前项吗', null, async () => {
            const { msg } = await doState({
              ProductID: row.ProductID,
              state: row.state,
            })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        if (route.query.classid) {
          state.queryForm.classid = route.query.classid
        }
        fetchData()
      })

      const taskQuantity = (row) => {
        if (row.ProductID) {
          state.taskEditListRef.showEdit(row, 2)
        } else {
          state.taskEditListRef.showEdit()
        }
      }

      const taskSetting = (row) => {
        if (row.ProductID) {
          state.taskEditListRef.showEdit(row, 1)
        } else {
          state.taskEditListRef.showEdit()
        }
        console.log(row, '任务设置')
      }

      return {
        ...toRefs(state),
        backupid,
        taskQuantity,
        taskSetting,
        getProduct,
        setSelectRows,
        handleEdit,
        handleState,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
